using System;
using System.Net;
using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Factories;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace AndyPike.Unfuddle.Tests.Unit.Factories
{
    [TestFixture]
    public class When_creating_a_http_web_request
    {
        const string url = "http://www.domain.com/";
        const string username = "my.username";
        const string password = "my.password";
        HttpWebRequestFactory factory;

        [SetUp]
        public void SetUp()
        {
            factory = new HttpWebRequestFactory();
        }

        [Test]
        public void Should_create_an_instance_for_valid_parameters()
        {
            HttpWebRequest request = factory.Create(url, username, password, HttpMethod.GET);

            Assert.That(request.Address.AbsoluteUri, Is.EqualTo(url));
            Assert.That(request.Headers["Authorization"], Is.Not.Null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_the_url_is_null()
        {
            HttpWebRequest request = factory.Create(null, username, password, HttpMethod.GET);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_the_username_is_null()
        {
            HttpWebRequest request = factory.Create(url, null, password, HttpMethod.GET);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_the_password_is_null()
        {
            HttpWebRequest request = factory.Create(url, username, null, HttpMethod.GET);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_an_exception_if_the_url_is_empty()
        {
            HttpWebRequest request = factory.Create(string.Empty, username, password, HttpMethod.GET);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_an_exception_if_the_username_is_empty()
        {
            HttpWebRequest request = factory.Create(url, string.Empty, password, HttpMethod.GET);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_an_exception_if_the_password_is_empty()
        {
            HttpWebRequest request = factory.Create(url, username, string.Empty, HttpMethod.GET);
        }
    }
}